Crispo - Excel Challenge 49 2024

excel-challenges
weekly-exercises
Easy Sunday Excel Challenge
Published

December 8, 2024

Illustration for Crispo - Excel Challenge 49 2024

Challenge Description

Easy Sunday Excel Challenge

⭐ Airline Route Numb Whole Route British Airways HEA - DUB

Solutions

library(tidyverse)
library(readxl)

path = "files/Excel Challenge Dec 8th.xlsx"
input = read_excel(path, range = "B2:D12")
test  = read_excel(path, range = "F2:G6")

result = input %>%
  pivot_wider(names_from = "Numb", values_from = "Route") %>%                                                                                                                                                                                                                                                                                                                  pivot_wider(names_from = "Numb", values_from = "Route") %>%
  pivot_longer(cols = c(2:5), names_to = "Numb", values_to = "Route") %>%
  filter(!is.na(Route)) %>%
  mutate(Route = ifelse(Numb == 1, Route, str_extract(Route, "\\- [A-Z]+"))) %>%
  summarise(`Whole Route` = paste0(Route, collapse = " "), .by = Airline) %>%
  arrange(Airline)

all.equal(result, test)
#> [1] TRUE
  • Logic:

    • Reads the workbook range needed for the challenge

    • Reshapes the data to the grain required by the task

    • Aggregates or ranks values at the correct grouping level

    • Builds the intermediate helper columns that drive the final answer

  • Strengths:

    • The R solution stays compact and mirrors the workbook logic closely.
  • Areas for Improvement:

    • The code assumes the workbook layout and named ranges remain stable.
  • Gem:

    • The best part of the solution is choosing a tidy intermediate shape before producing the final answer.
import pandas as pd

path = "files/Excel Challenge Dec 8th.xlsx"
input = pd.read_excel(path, usecols="B:D", skiprows=1, nrows=10)
test = pd.read_excel(path, usecols="F:G", skiprows=1, nrows=4)
test.columns = ["Airline", "Whole Route"]

pivot_wider = input.pivot(index='Airline', columns='Numb', values='Route')
pivot_longer = pd.melt(pivot_wider.reset_index(), id_vars=['Airline'], value_vars=pivot_wider.columns, var_name='Numb', value_name='Route')
pivot_longer = pivot_longer.sort_values(['Airline', 'Numb'])
pivot_longer = pivot_longer.dropna()

pivot_longer['Route'] = pivot_longer.apply(lambda row: row['Route'] if row['Numb'] == 1 else (row['Route'].split('-')[1] if len(row['Route'].split('-')) > 1 else row['Route']), axis=1)

result = pivot_longer.groupby('Airline')['Route'].apply(lambda x: ' -'.join(x)).reset_index()
result.columns = ["Airline", "Whole Route"]

print(result.equals(test)) # True
  • Logic:

    • Reads the workbook range needed for the challenge

    • Reshapes the data to the grain required by the task

    • Aggregates or ranks values at the correct grouping level

  • Strengths:

    • The Python version keeps the same rule in a direct pandas-oriented workflow.
  • Areas for Improvement:

    • As with the R version, any workbook layout change would require small adjustments.
  • Gem:

    • The implementation stays close to the stated challenge instead of adding unnecessary complexity.

Difficulty Level

This task is moderate:

  • It combines familiar Excel-style logic with at least one non-trivial reshape, grouping, or parsing step.

  • The answer depends on getting the output layout exactly right.